package web.beans;

import entity.Car.bo.CarBo;
import entity.User.bo.UserBo;
import entity.User.model.User;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by vanches on 9.1.15.
 */
public class AdminBean implements Serializable {

    private CarBo carBo;
    private UserBo userBo;
    private ArrayList<User> users;
    private ArrayList<User> newUser=new ArrayList<User>();

    @PostConstruct
    private void init(){
        users= (ArrayList<User>) userBo.getAllUser();
        for(int i=users.size(); i<users.size()-7; i--){
            newUser.add(users.get(i));
        }
    }

    private String foundContext(String param){
        FacesContext context=FacesContext.getCurrentInstance();
        Map<String, String> map = context.getExternalContext().getRequestParameterMap();
        return map.get(param);
    }


    public void deleteUser(){
        Integer id=new Integer(foundContext("id_user"));
        User temp;
        for(int i=0; i<users.size();i++){
            if(users.get(i).getId()==id){
                temp=userBo.login(users.get(i).getLogin(),users.get(i).getPass());
                userBo.delete(temp);
                users.remove(users.get(i));
            }

        }
    }



    //region GET and Set


    public CarBo getCarBo() {
        return carBo;
    }

    public void setCarBo(CarBo carBo) {
        this.carBo = carBo;
    }

    public UserBo getUserBo() {
        return userBo;
    }

    public void setUserBo(UserBo userBo) {
        this.userBo = userBo;
    }

    public ArrayList<User> getUsers() {
        return users;
    }

    public void setUsers(ArrayList<User> users) {
        this.users = users;
    }

    public ArrayList<User> getNewUser() {
        return newUser;
    }

    public void setNewUser(ArrayList<User> newUser) {
        this.newUser = newUser;
    }

    //endregion
}
